﻿using ESRI.ArcGIS.Carto;
using System.Windows.Forms;

namespace MxdLayerReader
{
	public partial class MainGUI : Form
	{
		public MainGUI()
		{
			InitializeComponent();
		}

		private void openMxd_Click(object sender, System.EventArgs e)
		{
			// 先清除listBox中内容
			layerNames.Items.Clear();
			// 打开文件
			OpenFileDialog dialog = new OpenFileDialog();
			dialog.Title = "打开mxd文件";
			dialog.Filter = "mxd文件|*.mxd";
			string filePath;
			if (dialog.ShowDialog() == DialogResult.OK)
			{
				filePath = dialog.FileName;
			}
			else
			{
				return;
			}
			IMapDocument doc = new MapDocument();
			fileNameLabel.Text = "正在打开文件...";
			Application.DoEvents();
			doc.Open(filePath);
			fileNameLabel.Text = "文件名：" + filePath.Substring(filePath.LastIndexOf("\\") + 1, filePath.Length - filePath.LastIndexOf("\\") - 1);
			// 获取地图（Data Frame）
			IMap map;
			for (int i = 0; i < doc.MapCount; i++)
			{
				map = doc.get_Map(i);
				// 获取各个图层
				for (int j = 0; j < map.LayerCount; j++)
				{
					layerNames.Items.Add(map.Name + " - " + map.get_Layer(j).Name);
				}
			}
		}
	}
}